﻿using Assessment.BLL;
using Assessment.Model;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index_Gauge_GaugeTestItemList : System.Web.UI.UserControl
{
    #region 属性
    public int GaugeTestId
    {
        get { return ViewState["GaugeTestId"] == null ? 0 : Convert.ToInt32(ViewState["GaugeTestId"]); }
        set { ViewState["GaugeTestId"] = value; }
    }

    public int OrderNo
    {
        get { return ViewState["OrderNo"] == null ? 0 : Convert.ToInt32(ViewState["OrderNo"]); }
        set { ViewState["OrderNo"] = value; }
    }

    public int GaugeRecordAnswerId
    {
        get { return ViewState["GaugeRecordAnswerId"] == null ? -1 : Convert.ToInt32(ViewState["GaugeRecordAnswerId"]); }
        set { ViewState["GaugeRecordAnswerId"] = value; }
    }

    public string GaugeTestTitle
    {
        get { return ViewState["GaugeTestTitle"] == null ? "" : ViewState["GaugeTestTitle"].ToString(); }
        set { ViewState["GaugeTestTitle"] = value; }
    }

    public string GridValue
    {
        get { return ViewState["GridValue"] == null ? "" : ViewState["GridValue"].ToString(); }
        set { ViewState["GridValue"] = value; }
    }

    public string ItemTypeNo
    {
        get { return ViewState["ItemTypeNo"] == null ? "" : ViewState["ItemTypeNo"].ToString(); }
        set { ViewState["ItemTypeNo"] = value; }
    }

    public string AnswerValue
    {
        get { return this.hf_Value.Value; }
        set { this.hf_Value.Value = value; }
    }

    public string TitleTexts
    {
        get { return this.hf_TitleTextValue.Value; }
        set { this.hf_TitleTextValue.Value = value; }
    }
    #endregion

    T_GaugeTestItemBLL bll = new T_GaugeTestItemBLL();
    utilityBLL ubll = new utilityBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRepeaterList();
        }
    }

    private void BindRepeaterList()
    {
        List<T_GaugeTestItem> list = bll.GetModelList("Valid=1 and GaugeTestId=" + GaugeTestId);
        this.rpt_List.DataSource = list;
        this.rpt_List.DataBind();
    }

    #region 共用方法
    public string GetTypeHtmlByNo(int GaugeTestItemId, string ItemTypeNo, string ItemValue)
    {
        StringBuilder strHtml = new StringBuilder();
        strHtml.Clear();
        string strChecked = "";
        string strTextBox = "";
        switch (ItemTypeNo)
        {
            case "RADIO":
            case "RADIO_TEXT":
                #region 选择题
                string[] radio_text_values = AnswerValue.Split('\t');
                if (GaugeTestItemId.ToString() == radio_text_values[0])
                {
                    strChecked = "checked='checked'";
                    if (ItemTypeNo == "RADIO_TEXT")
                    {
                        strTextBox = (radio_text_values.Length > 1 ? radio_text_values[1] : "");
                    }
                }
                strHtml.Append("<input type='radio' name='GaugeTestId_" + GaugeTestId + "' value='" + GaugeTestItemId + "' " + strChecked + "/>");
                strHtml.Append("<label>" + ItemValue + "</label>");
                if (ItemTypeNo == "RADIO_TEXT")
                {
                    strHtml.Append("<input type='text' style='width:200px' value='" + strTextBox + "' maxlength='50'/>");
                }
                break;
                #endregion
            case "CHECKBOX":
            case "CHECKBOX_TEXT":
                #region 复选按钮
                Dictionary<int, string> dic = new Dictionary<int, string>();
                foreach (string row in AnswerValue.Split('\n'))
                {
                    if (row.Length > 0)
                    {
                        string[] vals = row.Split('\t');
                        if (vals.Length > 0 && dic.ContainsKey(Convert.ToInt32(vals[0])))
                        {
                            dic.Add(Convert.ToInt32(vals[0]), (vals.Length > 1 ? vals[1] : ""));
                        }
                    }
                }
                if (dic.ContainsKey(GaugeTestItemId))
                {
                    strChecked = "checked='checked'";
                    if (ItemTypeNo == "CHECKBOX_TEXT")
                    {
                        strTextBox = dic.ContainsKey(GaugeTestItemId) ? dic[GaugeTestItemId] : "";
                    }
                }
                strHtml.Append("<input type='checkbox' name='GaugeTestId_" + GaugeTestId + "' value='" + GaugeTestItemId + "' " + strChecked + "/>");
                strHtml.Append("<label>" + ItemValue + "</label>");
                if (ItemTypeNo == "CHECKBOX_TEXT")
                {
                    strHtml.Append("<input type='text' style='width:200px' value='" + strTextBox + "' maxlength='50'/>");
                }
                break;
                #endregion
            case "AREATEXT":
                strHtml.Append("<textarea  style='width: 400px; height: 50px;'>" + AnswerValue + "</textarea>");
                break;
            case "STEP_GRID":
                List<List<StepGrid>> GridHeadList = JsonConvert.DeserializeObject<List<List<StepGrid>>>(GridValue);
                List<List<StepGrid>> GridItemList = JsonConvert.DeserializeObject<List<List<StepGrid>>>(ItemValue);

                strHtml.Append("<table class='ui-step-grid'>");
                //表头
                strHtml.Append("<thead>");
                for (int row = 0; row < GridHeadList.Count; row++)
                {
                    strHtml.Append("<tr>");
                    for (int col = 0; col < GridHeadList[row].Count; col++)
                    {
                        int colspan = Convert.ToInt32(GridHeadList[row][col].colspan);
                        int rowspan = Convert.ToInt32(GridHeadList[row][col].rowspan);
                        string text = GridHeadList[row][col].text;
                        if (colspan > 0 && rowspan > 0 && text != "")
                        {
                            strHtml.Append("<th colspan='" + colspan + "' rowspan='" + rowspan + "' style='width:" + GridHeadList[row][col].width + "px'>"
                                 + text + "</th>");
                        }
                    }
                    strHtml.Append("</tr>");
                }
                strHtml.Append("</thead>");
                int maxColCount = 0;
                for (int row = 0; row < GridHeadList.Count; row++)
                {
                    maxColCount = maxColCount < GridHeadList[row].Count ? GridHeadList[row].Count : maxColCount;
                }

                string[] arrRowValues = AnswerValue.Split('\n');

                strHtml.Append("<tbody>");
                for (int row = 0; row < GridItemList.Count; row++)
                {
                    string[] arrColValues = (arrRowValues.Length > row ? arrRowValues[row].Split('\t') : null);
                    string strClass = (row % 2 == 0 ? "" : " class='old'");
                    strHtml.Append("<tr" + strClass + ">");
                    for (int col = 0; col < maxColCount; col++)
                    {
                        if (col < GridItemList[row].Count)
                        {
                            int colspan = Convert.ToInt32(GridItemList[row][col].colspan);
                            int rowspan = Convert.ToInt32(GridItemList[row][col].rowspan);
                            string text = GridItemList[row][col].text;
                            if (colspan > 0 && rowspan > 0 && text != "")
                            {
                                strHtml.Append("<td colspan='" + colspan + "' rowspan='" + rowspan + "'>" + text + "</td>");
                            }
                        }
                        else
                        {
                            int valIndex = col - GridItemList[row].Count;
                            string val = (arrColValues.Length > valIndex ? arrColValues[valIndex] : "");
                            strHtml.Append("<td><input type='text' style='width:40px' value='" + val + "'/></td>");
                        }
                    }
                    strHtml.Append("</tr>");
                }
                strHtml.Append("</tbody>");
                strHtml.Append("</table>");

                break;
            default:
                strHtml.Append("<b>空</b>");
                break;
        }
        return strHtml.ToString();
    }

    public string GetGaugeTestTitleHtml(string text)
    {
        string html = text;
        html = html.Replace("\n", "<br/>");
        return html;
    }
    #endregion
}

public class StepGrid
{
    public string text { get; set; }
    public string colno { get; set; }
    public string colspan { get; set; }
    public string rowspan { get; set; }
    public string width { get; set; }
    public string visible { get; set; }
}